package per.yhh.xxxtsj.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import per.yhh.xxxtsj.Utils.JWTUtils;
import per.yhh.xxxtsj.entity.Message;
import per.yhh.xxxtsj.entity.User;
import per.yhh.xxxtsj.service.MyorderService;
import per.yhh.xxxtsj.service.UserService;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yhh
 * @since 2022-04-17
 */
@RestController
@RequestMapping("/order")
public class MyorderController {

    @Autowired
    MyorderService myorderService;
    @Autowired
    UserService userService;
    @GetMapping("/order")
    public Message goods(HttpServletRequest request, @RequestParam("query") String keyword, @RequestParam("pagenum") Integer pageNum, @RequestParam("pagesize") Integer pageSize) {
        String authorization = request.getHeader("Authorization");
        String account = JWTUtils.validateToken(authorization);
        User user = userService.getUserByAccount(account);
        if (user == null) return Message.failed();
        Integer permissions = user.getPermissions();
        if(permissions==2||permissions==3){
            return Message.ok(myorderService.getOrderByPageAndKeyAndId(user.getUid(),keyword,pageNum,pageSize));
        }
        return Message.ok(myorderService.getOrderByPage(keyword, pageNum, pageSize));
    }

}

